<template>
	<view class="home">
		<!-- 轮播图区域 -->
		<swiper class="swiper" indicator-dots autoplay circular>
			<swiper-item v-for="item in swiperListData" :key="item.goods_id">
				<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id"><image :src="item.image_src"></image></navigator>
			</swiper-item>
		</swiper>

		<!-- 导航栏 -->
		<view class="nav">
			<view class="nav-item" v-for="item in navListData" :key="item.name" @click="clickNavItem(item)"><image :src="item.image_src"></image></view>
		</view>

		<!-- 楼层 -->
		<view class="floor">
			<view class="floor-item" v-for="(item, index) in floorListData" :key="index">
				<view class="floor-item-title"><image :src="item.floor_title.image_src" mode="widthFix"></image></view>
				<view class="floor-item-main">
					<viwe class="floor-item-main-left">
						<navigator :url="item.product_list[0].url">
							<image :src="item.product_list[0].image_src" :style="{ width: item.product_list[0].image_width + 'rpx' }" mode="widthFix"></image>
						</navigator>
					</viwe>
					<view class="floor-item-main-right">
						<block v-for="(item2, index2) in item.product_list" :key="index2" v-if="index2 !== 0">
							<navigator :url="item2.url"><image :src="item2.image_src" :style="{ width: item2.image_width + 'rpx' }" mode="widthFix"></image></navigator>
						</block>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 轮播图数据
			swiperListData: [],
			// 导航栏数据
			navListData: [],
			// 楼层数据
			floorListData: []
		};
	},
	onLoad() {
		// 获取轮播图数据
		this.getSwiperList();
		// 获取导航栏数据
		this.getNavList();
		// 获取楼层数据
		this.getFloorList();
	},
	methods: {
		// 获取轮播图数据
		async getSwiperList() {
			const { data: res } = await uni.$http.get('/api/public/v1/home/swiperdata');
			if (res.meta.status !== 200) return uni.$showMsg();

			this.swiperListData = res.message;
			// console.log(this.swiperListData);
		},
		// 获取导航栏数据
		async getNavList() {
			const { data: res } = await uni.$http.get('/api/public/v1/home/catitems');
			if (res.meta.status !== 200) return uni.$showMsg();
			this.navListData = res.message;
			// console.log(this.navListData)
		},
		// 获取楼层数据
		async getFloorList() {
			const { data: res } = await uni.$http.get('/api/public/v1/home/floordata');
			if (res.meta.status !== 200) return uni.$showMsg();
			res.message.forEach(item => {
				item.product_list.forEach(item => {
					item.url = `/subpkg/goods_list/goods_list?${item.navigator_url.split('?')[1]}`;
				});
			});
			this.floorListData = res.message;
			console.log(this.floorListData);
		},
		// 点击导航栏每项触发
		clickNavItem(item) {
			if (item.name === '分类') {
				uni.switchTab({
					url: '../classify/classify'
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.home {
	.swiper {
		width: 100%;
		height: 330rpx;
		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav {
		display: flex;
		width: 750rpx;
		.nav-item {
			display: flex;
			flex: 1;
			justify-content: center;
			padding: 25rpx 0;
			image {
				width: 128rpx;
				height: 140rpx;
			}
		}
	}

	.floor {
		width: 100%;
		.floor-item {
			.floor-item-title {
				image {
					width: 100%;
				}
			}
			.floor-item-main {
				display: flex;
				.floor-item-main-right {
					display: flex;
					flex-wrap: wrap;
					image {
						padding-left: 20rpx;
					}
				}
			}
		}
	}
}
</style>
